﻿using System;
using System.Collections.Generic;
using System.Text;
using RockCompany.Common.Lib;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using RockCompany.Common.Constant;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using System.Windows.Forms;
using RockCompany.Master.Model;
using RockCompany.Master.View;
using RockCompany.Master.Validator;

namespace RockCompany.Master.Controller
{
    public class SW01FormCtrl
    {
        private SW01FormModel modelSW01 = new SW01FormModel();
     
        public SW01FormCtrl()
        {
            
        }

        public void addWOTypeToCombo(ComboBox cb)
        {
            cb.Items.Add(SW01AConst.CB_NEW_TYPE);
            cb.Items.Add(SW01AConst.CB_MODIFICATION_TYPE);
            cb.SelectedIndex=0;
        }

        public DataTable SearchWO(WorkOrderDTO woDTO)
        {
            DataTable res = modelSW01.SearchWO(woDTO);

            foreach (DataRow row in res.Rows)
            {
                if (row[SW01AConst.DIEU_CHINH].Equals(SW01AConst.NEW_TYPE.ToString()))
                {
                    row[SW01AConst.DIEU_CHINH] = SW01AConst.CB_NEW_TYPE;
                }
                else
                {
                    row[SW01AConst.DIEU_CHINH] = SW01AConst.CB_MODIFICATION_TYPE;
                }
            }

            return res;
        }

        public bool LoadWO(SW01A sW01A, string choosenLotNo)
        {
            modelSW01.translator = sW01A.translator;
            WorkOrderDTO woDTO=new WorkOrderDTO(choosenLotNo);
            DataTable wo = SearchWO(woDTO);

            if (CommonModel.existData(wo))
            {
                DataRow row = wo.Rows[0];
                string woType = Util.parseString(row[SW01AConst.DIEU_CHINH]);

                woDTO.WOType = SW01AConst.NEW_TYPE;
                if (woType.Equals(SW01AConst.CB_MODIFICATION_TYPE))
                {
                    woDTO.WOType = SW01AConst.MODIFICATION_TYPE;
                }

                DataTable woDetails = modelSW01.SearchWODetails(woDTO);
                if (!CommonModel.existData(woDetails)) { return false; }
                
                // Calculate sum of product
                DataTable proQty = modelSW01.CalculateProductQty(choosenLotNo);

                // Bind data to form
                sW01A.cbWOType.Items.Add(woType);
                sW01A.cbWOType.SelectedIndex = 0;
                bool pheDuyet = false;
                if (Boolean.TryParse(row[SW01AConst.PHE_DUYET].ToString(), out pheDuyet))
                {
                    sW01A.checkBox_Pass.Checked = pheDuyet;
                }

                sW01A.tbLotNo.Text = Util.parseString(row[SW01AConst.SO_LO]);
                sW01A.tbProductNo.Text = Util.parseString(row[SW01AConst.MA_MAU]);
                sW01A.cbParentLotNo.Items.Add(Util.parseString(row[SW01AConst.CONG_LENH_CHA]));
                sW01A.cbParentLotNo.SelectedIndex = 0;
                sW01A.picProductionDate.Value = Util.parseDateTime(row[SW01AConst.NGAY_SX]);

                sW01A.tbDoMauN.Text = Util.parseString(row[SW01AConst.DO_MAU_N]);
                sW01A.tbDoMauS.Text = Util.parseString(row[SW01AConst.DO_MAU_S]);
                sW01A.tbDoSangN.Text = Util.parseString(row[SW01AConst.DO_SANG_N]);
                sW01A.tbDoSangS.Text = Util.parseString(row[SW01AConst.DO_SANG_S]);
                sW01A.tbMauN.Text = Util.parseString(row[SW01AConst.MAU_N]);
                sW01A.tbMauS.Text = Util.parseString(row[SW01AConst.MAU_S]);

                sW01A.tbHieuSuat.Text = Util.parseString(row[SW01AConst.HIEU_SUAT]);
                sW01A.tbSLTonAT.Text = Util.parseString(row[CONG_LENH_SX.N_SL_TON_AN_TOAN]);
                sW01A.tbGiaCL.Text = Util.FormatDecimal(row[CONG_LENH_SX.N_TONG_TIEN]);

                if (proQty != null && proQty.Rows.Count > 0)
                {
                    sW01A.tbTongSLNK.Text = Util.parseString(proQty.Rows[0][PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP]);
                    sW01A.tbNgayNK.Text = Util.FormatDate(Util.parseDateTime(proQty.Rows[0][PHIEU_NK_TP_CT_DTO.N_CREATION_TIME]));
                }
                

                List<string> columnNames = new List<string>();
                foreach (DataColumn col in sW01A.matList.Columns)
                {
                    if (!col.ColumnName.Equals(NGUYEN_VLIEU_VTU_DTO.N_CHON))
                    {
                        columnNames.Add(col.ColumnName);
                    }
                }

                // Remove don gia va don vi tien te
                if (woDTO.WOType == SW01AConst.MODIFICATION_TYPE)
                {
                    columnNames.Remove(NGUYEN_VLIEU_VTU_DTO.N_DON_GIA);
                    columnNames.Remove(NGUYEN_VLIEU_VTU_DTO.N_TY_GIA);
                }

                modelSW01.copyDataTable(woDetails, sW01A.matList, columnNames);

                // Set rowstate modified to all rows
                sW01A.matList.AcceptChanges();
                foreach (DataRow modRow in sW01A.matList.Rows)
                {
                    modRow.SetModified();
                }

                sW01A.dgvMaterialList.DataSource = sW01A.matList;
                bool isVisible = row[SW01AConst.DIEU_CHINH].Equals(SW01AConst.CB_NEW_TYPE);

                sW01A.dgvMaterialList.Columns[CONG_LENH_SX_CT.N_SO_LAN].Visible = isVisible;
                
                return true;
            }

            return false;
        }
    }
}
